package gw.com.android.ui.bulletin;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import gw.com.android.app.AppContances;
import gw.com.android.app.GTConfig;
import gw.com.android.utils.NotificationUtil;
import gw.com.android.utils.PackStatis;

/**
 * @author jett
 * @since 2018-08-16.
 */
public class NotifyEmptyAdapter extends RecyclerView.Adapter {

    private static final int TYPE_OPEN_SYS = 1;

    private Activity activity;
    private boolean showOpenSys;

    NotifyEmptyAdapter(Activity activity, boolean showOpenSys) {
        this.activity = activity;
        this.showOpenSys = showOpenSys;
    }

    public void setShowOpenSys(boolean showOpenSys) {
        this.showOpenSys = showOpenSys;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_OPEN_SYS) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_notify_open_sys, parent, false);
            return new OpenSysHolder(itemView);
        } else {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_notify_centre_empty, parent, false);
            return new EmptyHolder(itemView);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    }

    @Override
    public int getItemCount() {
        return 5 + (showOpenSys ? 1 : 0);
    }

    @Override
    public int getItemViewType(int position) {
        if (showOpenSys && position == 0) {
            return TYPE_OPEN_SYS;
        }
        return super.getItemViewType(position);
    }

    private class EmptyHolder extends RecyclerView.ViewHolder {

        EmptyHolder(View itemView) {
            super(itemView);
        }

    }

    class OpenSysHolder extends RecyclerView.ViewHolder {

        OpenSysHolder(View itemView) {
            super(itemView);
            ImageView ivOpenSysClose = itemView.findViewById(R.id.notice_centre_opensys_close);
            TextView tvOpenSysOpen = itemView.findViewById(R.id.notice_centre_opensys_open);
            ivOpenSysClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PackStatis.getToService("clickClose", "Me", null, null);
                    GTConfig.instance().setBooleanValue("notify_open_sys_showed", true);
                    setShowOpenSys(false);
                    notifyDataSetChanged();
                }
            });
            tvOpenSysOpen.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PackStatis.getToService("clickOpen", "Me", null, null);
                    NotificationUtil.toNotificationSetting(activity, AppContances.REQUEST_SYSTEM_PUSH_CODE);
                }
            });
        }

    }

}